package com.cee.admin.modules.sys.controller;

import com.cee.admin.common.BaseController;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.sys.entity.Menu;
import com.cee.domain.modules.sys.vo.MenuVo;
import com.cee.ds.modules.sys.service.IMenuService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * ϵͳ 前端控制器
 * </p>
 *
 * @author zcj
 * @since 2020-12-07
 */
@Tag(name = "系统权限管理")
@RestController
@RequestMapping("/api/v1/sys/menu")
public class MenuController extends BaseController {

    @Autowired
    private IMenuService menuService;


    @Operation(summary = "获取菜单树")
    @GetMapping("getTree")
    @RequiresPermissions(value = "sys:menu:select")
    public Result<List<MenuVo>> getTree() {
        try {
            return success(menuService.tree());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error("系统错误，请联系管理员！");
        }
    }

    /**
     * 获取菜单
     *
     * @return
     */
    @Operation(summary = "根据父级节点获取菜单",description = "不传父级id则默认为根节点-e的子节点")
    @GetMapping("/getMenuList")
    @RequiresPermissions(value = "sys:menu:select")
    public Result<List<Menu>> getMenuList(@Parameter(description = "父级id（根节点为0）") Long parentId) {
        return success(menuService.queryListByParentId(parentId));
    }

    /**
     * 更新菜单
     *
     * @param menu
     * @return
     */
    @Operation(summary = "更新菜单")
    @PostMapping("/updateMenu")
    @RequiresPermissions(value = "sys:menu:update")
    public Result<Boolean> updateMenu(@RequestBody Menu menu) {
        return success(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     *
     * @param id 删除菜单的id
     * @return
     */
    @Operation(summary = "删除菜单")
    @GetMapping("/delMenu/{id}")
    @RequiresPermissions(value = "sys:menu:delete")
    public Result<Boolean> delMenu(@PathVariable(value = "id") Long id) {
        return success(menuService.delMenu(id));
    }

    /**
     * 添加下级菜单
     *
     * @param menu
     * @return
     */
    @Operation(summary = "添加下级菜单",description = "不传父级id则默认为根节点0的子节点")
    @PostMapping("/addChildrenMenu")
    @RequiresPermissions(value = "sys:menu:insert")
    public Result<Boolean> addChildrenMenu(@RequestBody Menu menu) {
        return success(menuService.addChildrenMenu(menu));
    }

}

